package com.wanmao.test.adapter;

/**
 * Created by Administrator on 2017/12/13 0013.
 */

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wanmao.test.R;
import com.wanmao.test.Test_XiZeActivity;
import com.wanmao.test.bean.XMBean;
import com.wanmao.test.bean.YDBean;
import com.wanmao.test.util.PerferenceUntil;


import java.util.List;

public class MainAdapter extends BaseExpandableListAdapter {

    private List<XMBean> list;
    private Context context;
    private boolean star=true;

    public MainAdapter(List<XMBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    public MainAdapter() {
    }
    public String RWID;

    public String getRWID() {
        return RWID;
    }

    public void setRWID(String RWID) {
        this.RWID = RWID;
    }
    @Override
    public int getGroupCount() {
        return list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition).getList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).getList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        GroupHolder holder;
        if (convertView == null) {
            holder = new GroupHolder();
            convertView = LayoutInflater.from(context).inflate(                    R.layout.item_group, null);
            holder.title = (TextView) convertView.findViewById(R.id.group_title);
            holder.tv_number = (TextView) convertView.findViewById(R.id.tv_number);
            holder.iv = (ImageView) convertView.findViewById(R.id.group_ico);
            convertView.setTag(holder);
        } else {
            holder = (GroupHolder) convertView.getTag();
        }
        holder.title.setText(list.get(groupPosition).getXMMC());
        if (isExpanded) {
            holder.iv.setImageResource(R.drawable.rounds_open);
        } else {
            holder.iv.setImageResource(R.drawable.rounds_close);
        }
        int i = list.get(groupPosition).getList().size();
        int istrue = 0;
        String ID = list.get(groupPosition).getXM_ID()+RWID;
        SharedPreferences sp_number = context.getSharedPreferences(ID, context.MODE_PRIVATE);
        istrue = sp_number.getInt("istrue", istrue);
        holder.tv_number.setText(istrue + "/" + i);
        if (istrue==i){

        }else {
            if (star==true){
                star=false;
                Test_XiZeActivity.isfist=groupPosition;
            }
        }
        String XM_ID;
        int number=0;
        boolean isOne;
        boolean isfist;
        SharedPreferences sp = context.getSharedPreferences(list.get(groupPosition).getXM_ID()+RWID, context.MODE_PRIVATE);
        XM_ID=sp.getString("XM_ID","");
        isOne=sp.getBoolean("isOne",true);
        isfist=sp.getBoolean("isfist",true);
        SharedPreferences sp_numbers=context.getSharedPreferences(list.get(groupPosition).getXZID()+RWID,context.MODE_PRIVATE);
        number=sp_numbers.getInt("end_number",number);
        Log.e("end_number", "number: "+number+" XM_ID:"+XM_ID+" isOne:"+isOne+" isfist:"+isfist );
        if (istrue == i) {
            if (!isOne == true) {
                if (!list.get(groupPosition).getXM_ID().equals(XM_ID)) {
                    if (isfist == true) {
                        isfist=false;
                        isOne=false;
                        number=number+1;
                        PerferenceUntil.shareXZ(list.get(groupPosition).getXM_ID()+RWID,context,number,XM_ID,isOne,isfist);
                        PerferenceUntil.shareend(list.get(groupPosition).getXZID()+RWID,context,number);
                    }
                }

            }else {
                isOne=false;
                isfist=false;
                number=number+1;
                PerferenceUntil.shareXZ(list.get(groupPosition).getXM_ID()+RWID,context,number,XM_ID,isOne,isfist);
                PerferenceUntil.shareend(list.get(groupPosition).getXZID()+RWID,context,number);

            }
        }

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder holder;
        if (convertView == null) {
            holder = new ChildHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_project_details, null);
            holder.tv_title = (TextView) convertView.findViewById(R.id.item_tv_project_details);
            holder.tv_all = (TextView) convertView.findViewById(R.id.item_tv_project_details_all_true);
            holder.img_result = (ImageView) convertView.findViewById(R.id.item_img_prject_result);
            holder.layout_background = (LinearLayout) convertView.findViewById(R.id.layout_project_details);
            holder.layout_biaoti = (LinearLayout) convertView.findViewById(R.id.item_layout_biaoti);
            convertView.setTag(holder);
        } else {
            holder = (ChildHolder) convertView.getTag();
        }
        YDBean cb = list.get(groupPosition).getList().get(childPosition);

        SharedPreferences sp = context.getSharedPreferences(RWID+cb.getYD_ID(), context.MODE_PRIVATE);
        String result = sp.getString("result", "");
        if ("".equals(result)) {
            holder.img_result.setBackgroundResource(R.mipmap.img_item_weijiancha);
        } else if ("合格".equals(result)) {
            holder.img_result.setVisibility(View.VISIBLE);
            holder.img_result.setBackgroundResource(R.mipmap.img_item_hege);
        } else if ("不合格".equals(result)) {
            holder.img_result.setVisibility(View.VISIBLE);
            holder.img_result.setBackgroundResource(R.mipmap.img_item_buhege);
        } else {
            holder.img_result.setVisibility(View.VISIBLE);
            holder.img_result.setBackgroundResource(R.mipmap.img_item_busheji);
        }


        holder.tv_title.setText(cb.getYDMC());
        int i = childPosition + 1;
        if (i % 2 == 0) {
            holder.layout_background.setBackgroundResource(R.drawable.list_share_bule);
        } else {
            holder.layout_background.setBackgroundResource(R.drawable.list_share_green);
        }
        if (i % 3 == 0) {
            holder.layout_background.setBackgroundResource(R.drawable.list_share_three);
        }
        if (i == 1) {
            holder.layout_biaoti.setVisibility(View.VISIBLE);
        } else {
            holder.layout_biaoti.setVisibility(View.GONE);
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public void setRWID(MainAdapter adapter) {
    }

    class GroupHolder {
        TextView title;
        TextView tv_number;
        ImageView iv;
    }

    class ChildHolder {
        TextView tv_title;
        TextView tv_all;
        LinearLayout layout_background;
        LinearLayout layout_biaoti;
        ImageView img_result;
    }
}